package lpl.com.xyd.test.manage.controller;

import lpl.com.xyd.test.manage.common.response.HttpResponse;
import lpl.com.xyd.test.manage.common.utils.ContentType;
import lpl.com.xyd.test.manage.facade.UserFacade;
import lpl.com.xyd.test.manage.form.*;
import lpl.com.xyd.test.manage.info.UserInfo;
import lpl.com.xyd.test.manage.order.UserDelOrder;
import lpl.com.xyd.test.manage.order.UserModifyPassWordOrder;
import lpl.com.xyd.test.manage.order.UserNewOrder;
import lpl.com.xyd.test.manage.order.UserQueryOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserFacade userFacade;

    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public String login(@RequestBody  LoginForm form){
        return "";
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<UserInfo> add(@RequestBody UserAddForm form){
        LOGGER.info("新增用户");
        UserNewOrder order = new UserNewOrder();
        BeanUtils.copyProperties(form,order);
        return userFacade.addUser(order);
    }

    @RequestMapping(value = "/modify",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<UserInfo> modify(@RequestBody UserModifyPasswordForm form){
        LOGGER.info("用户修改密码");
        UserModifyPassWordOrder order = new UserModifyPassWordOrder();
        BeanUtils.copyProperties(form,order);
        order.setOldPassWord(form.getPassWord());
        return userFacade.modifyPassWord(order);
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<List<UserInfo>> query(@RequestBody UserQueryForm form){
        LOGGER.info("查询用户");
        UserQueryOrder order = new UserQueryOrder();
        BeanUtils.copyProperties(form,order);
        return userFacade.queryUser(order);
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<UserInfo> del(@RequestBody UserDelForm form){
        LOGGER.info("查询用户");
        UserDelOrder order = new UserDelOrder();
        BeanUtils.copyProperties(form,order);
        return userFacade.delUser(order);
    }
}
